package com.ailiwu.www.ailiwu.utils;

import android.content.Context;
import android.util.DisplayMetrics;

/**
 * Author:    XiYoung
 * Version    V1.0
 * Date:      2015/12/1 19:55
 * Description:这是一个单位转换的工具类
 *              px 2 dp
 *              dp 2 px
 *              sp 2 dp
 *              pd 2 sp
 * Modification  History:
 * Date         	Author        		Version        	Description
 * -----------------------------------------------------------------------------------
 * 2015/12/1       XiYoung              1.0                   1.0
 * Why & What is modified:
 */
public class DimensionUtils {

    /**
     * px 转 dp
     *
     * @param context
     * @param pxValue
     * @return
     */
    public static float px2dp(Context context, float pxValue) {
        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
//        DisplayMetrics displayMetrics=new DisplayMetrics();
        return pxValue / displayMetrics.density;
    }

    /**
     * dp 转 px
     *
     * @param context
     * @param dpValue
     * @return
     */
    public static float dp2px(Context context, float dpValue) {
        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        return dpValue * displayMetrics.density;
    }

    /**
     * px 转 sp
     *
     * @param context
     * @param pxValue
     * @return
     */
    public static float px2sp(Context context, float pxValue) {
        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        return pxValue / displayMetrics.scaledDensity;
    }

    /**
     * sp转px
     *
     * @param context
     * @param spValue
     * @return
     */
    public static float sp2px(Context context, float spValue) {
        DisplayMetrics displaMetrics = context.getResources().getDisplayMetrics();
        return spValue * displaMetrics.scaledDensity;
    }
}
